package com.blb.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.entity.*;
import com.blb.service.*;
import com.blb.utils.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tb
 * @since 2020-05-25
 */
@Controller
@RequestMapping("/laser")
@SessionAttributes(value = { "codeList" ,"iOrderLaserList","orderLaserIds","sdfp_orderNo"})
public class OrderLaserController {

    @Autowired
    private IOrderService orderService;

    @Autowired
    private IOrderLaserService orderLaserService;

    @Autowired
    private ILaserPrinterService laserPrinterService;

    @Autowired
    private IStayProductionService stayProductionService;

    /**
     * 分页列表
     * @param start
     * @param length
     * @return
     */
    @RequiresPermissions("printercode")
    @ResponseBody
    @GetMapping("page")
    public Map<String, Object> page(Integer draw,Integer start, Integer length){
        String company = ShiroUtils.getCompany();
        Map<String, Object> pageMap = new HashMap<>();
        List<StayProduction> ilist = orderLaserService.Ilist(start, length,company);
        QueryWrapper<StayProduction> wrapper = new QueryWrapper<>();
        Map<String, Object> alleq = new HashMap<>();
        alleq.put("company_id",company);
        alleq.put("state","0");
        wrapper.allEq(alleq);

        pageMap.put("iTotalRecords",stayProductionService.count(wrapper));
        pageMap.put("iTotalDisplayRecords",stayProductionService.count(wrapper));
        pageMap.put("data",ilist);
        return pageMap;
    }



    /**
     * 模糊查询
     * @param orderNo
     * @return
     */
    @RequiresPermissions("printercode")
    @ResponseBody
    @GetMapping("query")
    public Map<String, Object> query(String orderNo){
        String company = ShiroUtils.getCompany();
        Map<String, Object> pageMap = new HashMap<>();
        List<StayProduction> stayProductions = orderLaserService.queryByOrderNo(orderNo,company);
        pageMap.put("iTotalRecords",stayProductions.size());
        pageMap.put("iTotalDisplayRecords",stayProductions.size());
        pageMap.put("data",stayProductions);
        return pageMap;
    }


    /**
     * 显示生成的激光码
     * @param orderId
     * @return
     */
    @RequiresPermissions("printercode")
    @RequestMapping("/showLaser")
    public String show(String orderId, Model model){
        model.addAttribute("orderId",orderId);
        return "/production/laser/addLaser";
    }

    @RequiresPermissions("printercode")
    @ResponseBody
    @PostMapping("RShowLaser")
    public Map<String, Object> show1(String orderId,Model model){
        String company = ShiroUtils.getCompany();
        Map<String, Object> pageMap = new HashMap<>();
        //根据订单号查询商品数量
        int sum = Integer.parseInt(orderService.getSumById(orderId));
        //根据数量生成对应的激光码
        List<OrderLaser> list = new ArrayList<>();
        for (int i = 0; i < sum; i++) {
            OrderLaser ol = new OrderLaser();
            ol.setOrderNo(orderId);
            ol.setOlCode(UUIDUtils.getIdByTopSix());
            ol.setOlStatus("待生成");
            list.add(ol);
        }
        model.addAttribute("codeList",list);
        pageMap.put("iTotalRecords",list.size());
        pageMap.put("iTotalDisplayRecords",list.size());
        pageMap.put("data",list);
        return pageMap;
    }



    /**
     * 生成激光码
     * @param model
     * @return
     */
    @RequiresPermissions("printercode")
    @ResponseBody
    @GetMapping("addCode")
    public Response addCode(Model model){
        List<OrderLaser> codeList = (List<OrderLaser>) model.getAttribute("codeList");
        orderLaserService.generate(codeList);
        return new Response();
    }

    /**
     * 携带订单编号跳转到
     * @param orderNo
     * @return
     */
    @RequiresPermissions("printercode")
    @GetMapping("fpdyj")
    public String fpdyj(String orderNo,Model model){
        model.addAttribute("COrderNo",orderNo);
        return "/production/laser/distributionPrinter";
    }


    /**
     * 分配打印机，根据当前订单编号列出所有订单激光码列表
     * @param start
     * @param length
     * @param orderNo
     * @return
     */
    @RequiresPermissions("printercode")
    @ResponseBody
    @PostMapping("fpdyj_page")
    public Map<String, Object> fpdyj_page(int start, int length, String orderNo){
        System.out.println(orderNo);
        Map<String, Object> pageMap = new HashMap<>();
        List<IOrderLaser> list = orderLaserService.getByOrderNo(orderNo);
        System.out.println(list);
        pageMap.put("iTotalRecords",list.size());
        pageMap.put("iTotalDisplayRecords",list.size());
        pageMap.put("data",list);
        return pageMap;
    }


    /**
     * 跳转到自动分配
     * @param Nos
     * @param model
     * @return
     */
    @RequiresPermissions("printercode")
    @GetMapping("zdfp")
    public String zdfp(String Nos,Model model){
        Nos = Nos.substring(0,Nos.length()-1);
        String[] ids = Nos.split(",");

        QueryWrapper<OrderLaser> wrapper = new QueryWrapper<>();
        wrapper.in("id",ids);
        List<OrderLaser> orderlist = orderLaserService.list(wrapper);

        List<IOrderLaser> iOrderLaserList = new ArrayList<>();

        QueryWrapper<LaserPrinter> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("company_id",ShiroUtils.getCompany());
        List<LaserPrinter> printerList = laserPrinterService.list(wrapper1);

        for(OrderLaser ol : orderlist){
            IOrderLaser Iol = new IOrderLaser();
            Iol.setOlStatus(ol.getOlStatus());
            Iol.setId(ol.getId());
            Iol.setOlCode(ol.getOlCode());
            Iol.setOrderNo(ol.getOrderNo());
            Iol.setPrinterId(printerList.get(0).getId());
            Iol.setPrinter(printerList.get(0).getLpNum());
            iOrderLaserList.add(Iol);
        }
        model.addAttribute("iOrderLaserList",iOrderLaserList);
        return "production/laser/fp_zd";
    }


    /**
     * 确认打印，修改打印机和状态
     * @param model
     * @return
     */
    @RequiresPermissions("printer:update")
    @ResponseBody
    @GetMapping("confirmPrinting")
    public Response confirmPrinting(Model model){
        List<IOrderLaser> list = (List<IOrderLaser>) model.getAttribute("iOrderLaserList");
        Response res = new Response();
        res.setRs(orderLaserService.confirmPrintingAndLog(list));
        return res;
    }


    /**
     * 跳转手动分配页面，并存储已选择的订单激光码id
     * @param model
     * @param ids
     * @return
     */
    @RequiresPermissions("printercode")
    @RequestMapping("sd_page")
    public String sd_page(Model model,String ids,String orderNo){
        String[] arr = ids.split(",");
        model.addAttribute("orderLaserIds",arr);
        model.addAttribute("sdfp_orderNo",orderNo);
        return "production/laser/fp_sd";
    }

    /**
     * 手动分配打印机
     * @param pid
     * @param model
     * @return
     */
    @RequiresPermissions("printercode")
    @ResponseBody
    @RequestMapping("sdfp")
    public Response sdfp(String pid,Model model){
        String[] arr = (String[]) model.getAttribute("orderLaserIds");
        String orderNo = (String) model.getAttribute("sdfp_orderNo");
        orderLaserService.distributionPrinterAndLog(arr,orderNo,pid);
        return new Response();
    }


}

